{% extends "layout/manage.html" %}
{% load static %}
{% load issues %}


{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md/css/editormd.css' %}">
    <link rel="stylesheet" href="{% static 'plugin/bootstrap-datepicker/dist/css/bootstrap-datepicker.css' %}">
    <link rel="stylesheet" href="{% static 'plugin/bootstrap-select/dist/css/bootstrap-select.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugin/select2/css/select2.min.css' %}">
    <style>
        .issues-list .number {
            width: 100px;
            text-align: center;
        }

        .issues-list .number a {
            font-weight: 500;
            padding: 0 10px;
        }

        .issues-list .issue .tags {
            padding: 10px 0;
        }

        .issues-list .issue .tags span {
            margin-right: 20px;
            display: inline-block;
            font-size: 12px;
        }

        .issues-list .issue .tags .type {
            color: white;
            padding: 1px 5px;
            border-radius: 50px;
        }

        .pd-0 {
            padding: 0 !important;
        }

        .editormd {
            margin-bottom: 0;
        }

        {# 筛选 的css #}
        .filter-area .item {
            margin-bottom: 15px;
        }

        .filter-area .item .title {
            padding: 5px 0;
        }

        .filter-area .item .check-list a {
            text-decoration: none;
            display: inline-block;
            min-width: 65px;
        }

        .filter-area .item .check-list label {
            font-weight: 200;
            font-size: 13px;

        }

        .filter-area .item .check-list a:hover {
            font-weight: 300;
        }

        .filter-area .item .check-list .cell {
            margin-right: 10px;
        }


    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid clearfix" style="padding:20px 0;">
        <div class="col-sm-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="fa fa-search" aria-hidden="true"></i>筛选
                </div>
                <div class="panel-body filter-area">
                    <div class="item">
                        <div class="title">问题类型</div>
                        <div class="check-list">
                            {% for item in issues_type_filter %}
                                {{ item }}
                            {% endfor %}
                        </div>
                    </div>
                    <div class="item">
                        <div class="title">状态</div>
                        <div class="check-list">
                            {% for item in status_filter %}
                                {{ item }}
                            {% endfor %}
                        </div>
                    </div>
                    <div class="item">
                        <div class="title">优先级</div>
                        <div class="check-list">
                            {% for item in priority_filter %}
                                {{ item }}
                            {% endfor %}
                        </div>
                    </div>
                    <div class="item">
                        <div class="title">指派者</div>
                        <div class="check-list">
                            {% for item in assign_filter %}
                                {{ item }}
                            {% endfor %}
                        </div>
                    </div>
                    <div class="item">
                        <div class="title">关注者</div>
                        <div class="check-list">
                            {% for item in attention_filter %}
                                {{ item }}
                            {% endfor %}
                        </div>
                    </div>

                </div>
            </div>
        </div>

        <div class="col-sm-9">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <i class="fa fa-question-circle" aria-hidden="true"> </i>问题
                </div>
                <div class="panel-body">
                    <a class=" btn btn-success btn-sm" data-toggle="modal" data-target="#addModal">新建问题</a>
                    <a class="btn btn-primary btn-sm" data-toggle="modal" data-target="#inviteModal">邀请成员</a>
                </div>
                <table>
                    <tbody class="issues-list">
                    {% for item in issue_object_list %}
                        <tr>
                            <td class="number">
                                <i class="fa fa-circle text-{{ item.priority }}"></i>
                                <a href="{% url 'web:manage:issues_detail' project_id=request.tracer.project.id issue_id=item.id %}"
                                   target="_blank">#{% issue_just item.id %}</a>
                            </td>
                            <td class="issue">
                                <div>
                                    <a target="_blank"
                                       href="{% url 'web:manage:issues_detail' project_id=request.tracer.project.id issue_id=item.id %}">{{ item.subject }}</a>
                                </div>
                                <div class="tags">
                                    <span class="type" style="background-color: #56b8eb">
                                        {{ item.issues_type }}
                                    </span>

                                    <span>
                                        <i class="fa fa-refresh" aria-hidden="true"></i>{{ item.get_status_display }}
                                    </span>
                                    <span>
                                        <i class="fa fa-user-o" aria-hidden="true"></i>{{ item.creator.username }}
                                    </span>
                                    <span>
                                        <i class="fa fa-hand-o-right" aria-hidden="true"></i>{{ item.assign.username }}
                                    </span>
                                    <span>
                                        <i class="fa fa-calendar" aria-hidden="true"></i>{{ item.end_date }}截止
                                    </span>
                                    <span>
                                        <i class="fa fa-clock-o" aria-hidden="true"></i>{{ item.update_datetime }} 更新
                                    </span>
                                </div>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
                {#                分页功能#}
                <nav aria-label="Page navigation" style="margin-top: 0;">
                    <ul class="pagination">
                        {{ page_html|safe }}
                    </ul>
                </nav>

            </div>
        </div>
    </div>

    </div>
    {# 新建问题的 modal对话框#}
    <div id="addModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
         aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                    <h4 class="modal-title">新建问题</h4>
                </div>
                <div class="modal-body" style="padding-right: 40px">

                    <form id="addForm" class="form-horizontal">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="{{ form.issues_type.id_for_label }}"
                                   class="col-sm-2 control-label">{{ form.issues_type.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.issues_type }}
                                </div>
                                <div class="error-msg">{{ form.issues_type.errors.0 }}</div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.subject.id_for_label }}"
                                   class="col-sm-2 control-label">{{ form.subject.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.subject }}
                                </div>
                                <div class="error-msg">{{ form.subject.errors.0 }}</div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.module.id_for_label }}"
                                   class="col-sm-2 control-label">{{ form.module.label }}</label>
                            <div class="col-sm-10">
                                <div>
                                    {{ form.module }}
                                </div>
                                <div class="error-msg">{{ form.module.errors.0 }}</div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.desc.id_for_label }}"
                                   class="col-sm-2 control-label">{{ form.desc.label }}</label>
                            <div class="col-sm-10">
                                <div id="editor">
                                    {{ form.desc }}
                                </div>
                                <div class="error-msg">{{ form.desc.errors.0 }}</div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.status.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.status.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div>
                                        {{ form.status }}
                                    </div>
                                    <div class="error-msg">{{ form.status.errors.0 }}</div>
                                </div>
                            </div>
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.priority.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.priority.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div>
                                        {{ form.priority }}
                                    </div>
                                    <div class="error-msg">{{ form.priority.errors.0 }}</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.assign.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.assign.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    {{ form.assign }}
                                    <div class="error-msg">{{ form.assign.errors.0 }}</div>
                                </div>
                            </div>
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.attention.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.attention.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    {{ form.attention }}
                                    <div class="error-msg">{{ form.attention.errors.0 }}</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.start_date.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.start_date.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div id="id_start_date">
                                        {{ form.start_date }}
                                    </div>
                                    <div class="error-msg">{{ form.start_date.errors.0 }}</div>
                                </div>
                            </div>
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.end_date.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.end_date.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div id="id_end_date">
                                        {{ form.end_date }}
                                    </div>
                                    <div class="error-msg">{{ form.end_date.errors.0 }}</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.mode.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.mode.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div>
                                        {{ form.mode }}
                                    </div>
                                    <div class="error-msg">{{ form.mode.errors.0 }}</div>
                                </div>
                            </div>
                            <div class="col-md-6 pd-0">
                                <label for="{{ form.parent.id_for_label }}"
                                       class="col-sm-4 control-label">{{ form.parent.label }}</label>
                                <div class="col-sm-8 clearfix">
                                    <div>
                                        {{ form.parent }}
                                    </div>
                                    <div class="error-msg">{{ form.parent.errors.0 }}</div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                    <button id="btnAdd" type="button" class="btn btn-primary">添 加</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade in" id="inviteModal" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    邀请成员
                </div>
                <div class="modal-body">
                    <form id="inviteForm">
                        {% csrf_token %}
                        {% for item in invite_form %}
                            <div class="form-group">
                                <label for="{{ item.id_for_label }}">{{ item.label }}</label>
                                <span>{% if item.help_text %}({{ item.help_text }}) {% endif %}</span>
                                {{ item }}
                                <span class="error-msg"></span>
                            </div>
                        {% endfor %}
                        <button type="button" class="btn btn-success" id="btnGenInviteCode">生成邀请码</button>
                    </form>
                    <div class="hide" id="inviteArea">
                        <hr>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-btn">
                                    <input type="button" value="邀请链接" class="btn btn-default">
                                </div>
                                <input type="text" class="form-control" id="inviteUrl">
                                <div class="input-group-btn">
                                    <input type="button" value="复制链接" class="btn btn-primary" id="btnCopyUrl">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor.md/editormd.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-datepicker/dist/js/bootstrap-datepicker.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-datepicker/dist/locales/bootstrap-datepicker.zh-CN.min.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-select/dist/js/bootstrap-select.min.js' %}"></script>
    <script src="{% static 'plugin/bootstrap-select/js/i18n/defaults-zh_CN.js' %}"></script>
    <script src="{% static 'plugin/select2/js/select2.min.js' %}"></script>
    <script src="{% static 'plugin/select2/js/i18n/zh-CN.js' %}"></script>
    <script>
        var WIKI_UPLOAD_URL = "{% url 'web:manage:wiki_upload'  project_id=request.tracer.project.id %}";
        var INVITE_URL = "{% url 'web:manage:invite_url'  project_id=request.tracer.project.id %}";
        $(function () {
            {#initMarkdown();#}
            initDatePicker();
            bindAddIssues();
            BindShowEvent();
            bindCheckboxFilter();
            bindSelect2();
            bindCreateInviteCode();
            bindCopyUrl();
        });

        /*
          为slect2绑定事件
     */
        function bindSelect2() {
            $(".c1").select2({}).on("select2:select", function (e) {
                // 选中某一项触发
                console.log(e.params.data);
                location.href = e.params.data.id;
            }).on("select2:unselect", function (e) {
                // 取消选中某一项触发
                location.href = e.params.data.id;
            });
        }


        /*
          为checkbox绑定事件，让其跳转到a标签中的href地址
     */
        function bindCheckboxFilter() {
            $(".filter-area").find(":checkbox").click(function () {
                location.href = $(this).parent().attr("href");
            })
        }


        /*
           弹出对话框的时候再去渲染markdown编辑器
     */
        function BindShowEvent() {
            $('#addModal').on('show.bs.modal', function (event) {
                // 弹出对话框的时候再去渲染markdown编辑器
                initMarkdown();
            });
        }

        /*
            初始化markdown编辑器
      */
        function initMarkdown() {
            editormd("editor", {
                placeholder: "请输入内容",
                height: 300,
                path: "{% static 'plugin/editor.md/lib/' %}",
                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png"],
                imageUploadURL: WIKI_UPLOAD_URL,
            })
        }

        /*
           初始化datepicker时间选择器
     */
        function initDatePicker() {
            $("#id_start_date input,#id_end_date input").datepicker({
                format: "yyyy-mm-dd",
                startDate: "0",
                language: "zh-CN",
                autoclose: true,

            });

        }

        /*
           添加问题
     */
        function bindAddIssues() {
            $("#btnAdd").click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url: "{% url 'web:manage:issues' project_id=request.tracer.project.id %}",
                    method: "POST",
                    data: $("#addForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            location.href = location.href;
                        } else {
                            $.each(res.error, function (index, value) {
                                $("#id_" + index).parent().next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        /*
         点击按钮，发送ajax请求，生成邀请码
    */
        function bindCreateInviteCode() {
            $("#btnGenInviteCode").click(function () {
                $.ajax({
                    url: INVITE_URL,
                    type: "POST",
                    data: $("#inviteForm").serialize(),
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            $("#inviteArea").removeClass("hide").find("#inviteUrl").val(res.data)
                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next(".error-msg").text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        /*
         添加复制链接功能
    */
        function bindCopyUrl() {
            $("#btnCopyUrl").click(function () {
                var textInput = $("#inviteUrl")[0];
                textInput.select();
                document.execCommand("Copy");
                alert("复制成功");
            })

        }
    </script>
{% endblock %}


